// pages/me/index.js
const defaultAvatarUrl = 'https://i.loli.net/2017/08/21/599a521472424.jpg'
const { $Toast } =  require('../../dist/base/index')

import {post} from '../../utils/http'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '游客',
      flag: '生活不止有诗与远方，还有田忌赛马！',
      sex: 1,
      age: 18,
      show: 224
    },
    mineAction:[
      {
        title: "我的行业",
        icon: "praise_fill",
        url: "",
        value: "",
        action:"star"
      },
      {
        title: "我的会议",
        icon: "computer_fill",
        url: "",
        value: "",
        action:"meeting"
      },
      {
        title: "我的课程",
        icon: "computer_fill",
        url: "",
        value: "",
        action:"course"
      },
      {
        title: "反馈中心",
        icon: "customerservice_fill",
        url: "",
        value: "非工作日9:00-24:00",
        action: "help"
      },
      {
        title: "退出登录",
        icon: "trash_fill",
        url: "/pages/login/index",
        value: "",
        action:"out"
      }
    ],
    actionFunc:{
      star:(e)=>{
        wx.navigateTo({
          url: `/pages/mine/minestar?type=industry`,
        })
        console.log("点赞")
      },
      meeting:(e)=>{
        wx.navigateTo({
          url: `/pages/mine/meeting`,
        })
      },
      course:(e)=>{
        wx.navigateTo({
          url: `/pages/mine/minestar?type=course`,
        })
      },
      help:(e)=>{
        $Toast({
            content: '客服小姐姐睡觉了。。。'
        });
      },
      out:(e)=>{
        post("/user/auth/logout").then(res=>{
          wx.showToast({
            title: '退出成功',
            icon: 'none'
          })
          wx.removeStorageSync('token')
          wx.redirectTo({
            url: '/pages/login/index',
          })
        }).catch(err=>{
          
        })
        
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setUserInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setUserInfo()
  },

  setUserInfo(){
    this.getTabBar().setData({
      current: 'mine'
    })
    const user = getApp().globalData.userInfo
    if(user){
        this.setData({
        "userInfo.avatarUrl": user.avatarUrl || defaultAvatarUrl,
        "userInfo.nickName": user.nickName || "游客"
      })
    }
  },

  onAction(e){
    this.data.actionFunc[e.currentTarget.dataset.act](e)
  },
  onFast(e){
    const url = e.currentTarget.dataset.url
    if(!url){
      return
    }
    if(url === "/pages/login/index"){
      wx.navigateTo({url})
    }else {
      wx.switchTab({url})
    }
  }
})